perf: Optimize oneOf validation and add schema/validator caching#1197
Merged
Conversation
Follow-up to #1196 targeting the remaining validation bottlenecks for large data cube files (profiled on a 135MB Biacore T200 .bme file with 35M float values across 917 measurements). 1. Fast oneOf for typed arrays (tDimensionArray/tMeasureArray): the default oneOf validator checks all branches then re-validates to confirm uniqueness. We short-circuit by inspecting the first element's type to pick the correct branch directly. 2. Fast oneOf for array-vs-object (tDimensionArray | tFunction): if the instance is a list it must be the array branch, skip the object check. 3. Cache schema JSON, RefResolver, and validator instances per schema path to avoid redundant disk I/O and object construction. 4. Eliminate redundant fields() call in unstructure — reuse the tuple from the iteration loop. Validation time: 12.8s → 5.0s. Total pipeline: ~38s → ~25s. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ajcariaga16
approved these changes
Apr 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #1196 targeting the remaining validation bottlenecks, identified via cProfile on a 135MB Biacore T200
.bmefile (917 measurements, 35M data points).oneOffor typed arrays —tDimensionArrayandtMeasureArrayareoneOfover 3 typed array refs (number/boolean/string). The defaultoneOfvalidator validates against the first matching branch, then re-validates against ALL remaining branches to confirm uniqueness. We short-circuit by inspecting the first element's type to select the correct branch directly. (~6s saved)oneOffor array-vs-object —oneOf[tDimensionArray, tFunction]patterns: if the instance is a list, it must be the array branch (tFunction is an object). Skip the redundant validation. (917 calls saved)RefResolver, andFastDraft202012Validatorinstances per schema path to avoid redundant disk I/O and object construction on repeated validations. (~1.7s saved on first call)fields()call —unstructure()calledfields()twice per dataclass (once for iteration, once to build a set for custom_information_document check). Reuse the tuple.Benchmark (135MB .bme file)
Test plan
.bmefile🤖 Generated with Claude Code